﻿// LTDMC_CalibPos_Dlg.cpp: 实现文件
//

#include "pch.h"
#include "SakerProject.h"
#include "LTDMC_CalibPos_Dlg.h"
#include "afxdialogex.h"

#include "xxFileProcess.h"
#include "xxWidgetControl.h"
#include "mmFile_Paras.h"


// CLTDMC_CalibPos_Dlg 对话框
CLTDMC_CalibPos_Dlg * m_LTDMC_CalibPos;

IMPLEMENT_DYNAMIC(CLTDMC_CalibPos_Dlg, CDialogEx)

CLTDMC_CalibPos_Dlg::CLTDMC_CalibPos_Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LTDMC_CalibPos, pParent)
{

}

CLTDMC_CalibPos_Dlg::~CLTDMC_CalibPos_Dlg()
{
}

void CLTDMC_CalibPos_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CLTDMC_CalibPos_Dlg, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_CalibPosName, &CLTDMC_CalibPos_Dlg::OnCbnSelchangeComboCalibposname)
	ON_BN_CLICKED(IDC_BUTTON_Save, &CLTDMC_CalibPos_Dlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_Move2CalibPos, &CLTDMC_CalibPos_Dlg::OnBnClickedButtonMove2calibpos)
	ON_NOTIFY(NM_CLICK, IDC_LIST_CalibPos, &CLTDMC_CalibPos_Dlg::OnNMClickListCalibpos)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_CalibPosNum, &CLTDMC_CalibPos_Dlg::OnDeltaposSpinCalibposnum)
END_MESSAGE_MAP()


// CLTDMC_CalibPos_Dlg 消息处理程序

BOOL CLTDMC_CalibPos_Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	xx::InitCB(this, IDC_COMBO_CalibPosName, mmLTDMC.vecCalibPosNames(), CurCalibPos);
	xx::WriteWidget(this, IDC_EDIT_CalibPosNum, mmLTDMC.CalibPosNum);

	vector<int> vecAxis = { IDC_STATIC_Axis0, IDC_STATIC_Axis1, IDC_STATIC_Axis2, IDC_STATIC_Axis3, IDC_STATIC_Axis4, IDC_STATIC_Axis5, IDC_STATIC_Axis6, IDC_STATIC_Axis7 };
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		SetDlgItemText(vecAxis[i], mmLTDMC.vecAxisNames[i]);
	}
	for (int i = mmLTDMC.AxisNum(); i < iiLTDMC_Axis_MaxNum; i++) {
		GetDlgItem(vecAxis[i])->ShowWindow(false);
	}

	mmLTDMC.InitCalibPosLC((CListCtrl*)GetDlgItem(IDC_LIST_CalibPos));

	InitCurCalibPos();

	((CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_CalibPosNum))->SetRange(iiLTDMC_CalibPos_MinNum, iiLTDMC_CalibPos_MaxNum);

	return TRUE;
}

void CLTDMC_CalibPos_Dlg::InitCurCalibPos() {
	CString iniField = mmLTDMC.vecCalibPosNames()[CurCalibPos];
	CString iniCurrentPath = mmFile_Paras.iniModelPath();

	vector<int> vecAxis = { IDC_EDIT_Axis0, IDC_EDIT_Axis1, IDC_EDIT_Axis2, IDC_EDIT_Axis3, IDC_EDIT_Axis4, IDC_EDIT_Axis5, IDC_EDIT_Axis6, IDC_EDIT_Axis7 };
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		xx::WriteWidget(this, vecAxis[i], mmLTDMC.vecCalibPosInfo[CurCalibPos][i]);
	}
	for (int i = mmLTDMC.AxisNum(); i < iiLTDMC_Axis_MaxNum; i++) {
		GetDlgItem(vecAxis[i])->ShowWindow(false);
	}


	((CListCtrl*)GetDlgItem(IDC_LIST_CalibPos))->SetFocus();
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		CString cstr;
		cstr.Format(L"%d", mmLTDMC.vecCalibPosInfo[CurCalibPos][i]);
		((CListCtrl*)GetDlgItem(IDC_LIST_CalibPos))->SetItemText(CurCalibPos, i + 1, cstr);
	}
	for (int i = 0; i < mmLTDMC.CalibPosNum; i++) {
		((CListCtrl*)GetDlgItem(IDC_LIST_CalibPos))->SetItemState(i, 0, -1);
	}
	((CListCtrl*)GetDlgItem(IDC_LIST_CalibPos))->SetItemState(CurCalibPos, LVNI_FOCUSED | LVIS_SELECTED, LVNI_FOCUSED | LVIS_SELECTED);
}


void CLTDMC_CalibPos_Dlg::OnCbnSelchangeComboCalibposname()
{
	CurCalibPos = ((CComboBox*)GetDlgItem(IDC_COMBO_CalibPosName))->GetCurSel();
	InitCurCalibPos();
}





void CLTDMC_CalibPos_Dlg::OnBnClickedButtonSave()
{
	CString iniField;
	iniField.Format(L"LTDMC_CalibPos_%02d... ...", CurCalibPos);
	CString iniCurrentPath = mmFile_Paras.iniModelPath();

	xx::Updateini(mmLTDMC.iniField, L"CalibPosNum", mmLTDMC.CalibPosNum, iniCurrentPath);

	vector<int> vecAxis = { IDC_EDIT_Axis0, IDC_EDIT_Axis1, IDC_EDIT_Axis2, IDC_EDIT_Axis3, IDC_EDIT_Axis4, IDC_EDIT_Axis5, IDC_EDIT_Axis6, IDC_EDIT_Axis7 };
	CString cstr;
	for (int i = 0; i < mmLTDMC.AxisNum(); i++) {
		cstr.Format(L"Axis_[%02d]", i);
		mmLTDMC.vecCalibPosInfo[CurCalibPos][i] = xx::DefaultWidget(this, vecAxis[i], iniField, cstr, iniCurrentPath, 0);
	}

	InitCurCalibPos(); // 将更新内容显示到 ListControl
}


void CLTDMC_CalibPos_Dlg::OnBnClickedButtonMove2calibpos()
{
	mmLTDMC.Move2CalibPos(CurCalibPos);
}


void CLTDMC_CalibPos_Dlg::OnNMClickListCalibpos(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

	HD_NOTIFY * phdn = (HD_NOTIFY*)pNMHDR;
	int SelCalibPos = phdn->iItem;
	if (SelCalibPos != -1) {
		CurCalibPos = SelCalibPos;
		((CComboBox*)GetDlgItem(IDC_COMBO_CalibPosName))->SetCurSel(CurCalibPos);
		InitCurCalibPos();
	}

	*pResult = 0;
}




void CLTDMC_CalibPos_Dlg::OnDeltaposSpinCalibposnum(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	if (pNMUpDown->iDelta == -1) { // 如果此值为-1 , 说明点击了Spin的往下的箭头
		mmLTDMC.DeleteCalibPos((CListCtrl*)GetDlgItem(IDC_LIST_CalibPos), (CComboBox*)GetDlgItem(IDC_COMBO_CalibPosName));
		CurCalibPos = mmLTDMC.CalibPosNum - 1;
	}
	else if (pNMUpDown->iDelta == 1) { // 如果此值为1, 说明点击了Spin的往上的箭头 
		mmLTDMC.AddCalibPos((CListCtrl*)GetDlgItem(IDC_LIST_CalibPos), (CComboBox*)GetDlgItem(IDC_COMBO_CalibPosName));
		CurCalibPos = mmLTDMC.CalibPosNum - 1;
	}
	InitCurCalibPos();
	*pResult = 0;
}
